package com.bdqn.tour.dao.impl;

import com.bdqn.tour.dao.BaseDao;
import com.bdqn.tour.dao.HzyOrdersDao;
import com.bdqn.tour.pojo.HzyHotel;
import com.bdqn.tour.pojo.HzyOrder;
import com.bdqn.tour.pojo.HzyScenicSpot;
import com.bdqn.tour.pojo.HzyTickets;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class HzyOrdersDaoImpl extends BaseDao implements HzyOrdersDao {
    @Override
    public List<HzyOrder> getAllHotelOrders() {
        conn = getConnection();
        List<HzyOrder> list = new ArrayList<>();
        String sql = "SELECT * FROM `biaodan`";
        try {
            rs = executeQuery(sql);
            while (rs.next()) {
                HzyOrder order = new HzyOrder();
                HzyHotel hotel = new HzyHotel();
                hotel.setId(rs.getInt("id"));
                hotel.setRoomNum(rs.getInt("roomnumbre"));
                hotel.setContactPhone(rs.getString("contactPhone"));
                hotel.setContactName(rs.getString("xmname"));
                hotel.setIdCard(rs.getString("sfztel"));
                hotel.setPrice(rs.getDouble("prices"));
                hotel.setStatus(rs.getString("state"));
                hotel.setBrand(rs.getString("brand"));
                order.setHotel(hotel);
                list.add(order);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            this.closeAll(conn, psmt, rs);
        }
        return list;
    }

    @Override
    public List<HzyOrder> getAllScenicOrders() {
        conn = getConnection();
        List<HzyOrder> list = new ArrayList<>();
        String sql = "SELECT * FROM `scenictable`";
        try {
            rs = executeQuery(sql);
            while (rs.next()) {
                HzyOrder order = new HzyOrder();
                HzyScenicSpot scenicSpot = new HzyScenicSpot();
                scenicSpot.setId(rs.getInt("scenic_Id"));
                scenicSpot.setScenicName(rs.getString("scenic_name"));
                scenicSpot.setScenicAddress(rs.getString("scenic_address"));
                scenicSpot.setPrice(rs.getDouble("price"));
                scenicSpot.setCity(rs.getString("city"));
                order.setScenicSpot(scenicSpot);
                list.add(order);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }

    @Override
    public List<HzyOrder> getAllTicketOrders() {
        conn = getConnection();
        List<HzyOrder> list = new ArrayList<>();
        String sql = "SELECT * FROM `tickets`";
        try {
            rs = executeQuery(sql);
            while (rs.next()) {
                HzyOrder order = new HzyOrder();
                HzyTickets tickets = new HzyTickets();
                tickets.setId(rs.getInt("id"));
                tickets.setType(rs.getString("type"));
                tickets.setTicketType(rs.getString("ticketType"));
                tickets.setDeparture_city(rs.getString("departure_city"));
                tickets.setArrival_city(rs.getString("arrival_city"));
                tickets.setDeparture_time(rs.getString("departure_date"));
                tickets.setStart_time(rs.getString("start_time"));
                tickets.setEnd_time(rs.getString("end_time"));
                tickets.setPrice(rs.getDouble("price"));
                tickets.setHas_infant(rs.getByte("has_infant_option"));
                tickets.setHas_child(rs.getByte("has_child_option"));
                order.setTickets(tickets);
                list.add(order);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }

    @Override
    public List<HzyOrder> getAllOrders() {
        conn = getConnection();
        List<HzyOrder> list = new ArrayList<>();
        try {
            // 获取酒店订单
            String hotelSql = "SELECT * FROM `biaodan`";
            rs = executeQuery(hotelSql);
            while (rs.next()) {
                HzyOrder order = new HzyOrder();
                HzyHotel hotel = new HzyHotel();
                hotel.setId(rs.getInt("id"));
                hotel.setRoomNum(rs.getInt("roomnumbre"));
                hotel.setContactPhone(rs.getString("contactPhone"));
                hotel.setContactName(rs.getString("xmname"));
                hotel.setIdCard(rs.getString("sfztel"));
                hotel.setPrice(rs.getDouble("prices"));
                hotel.setStatus(rs.getString("state"));
                hotel.setBrand(rs.getString("brand"));
                order.setHotel(hotel);
                list.add(order);
            }

            // 获取景点订单
            String scenicSql = "SELECT * FROM `scenictable`";
            rs = executeQuery(scenicSql);
            while (rs.next()) {
                HzyOrder order = new HzyOrder();
                HzyScenicSpot scenicSpot = new HzyScenicSpot();
                scenicSpot.setId(rs.getInt("scenic_Id"));
                scenicSpot.setScenicName(rs.getString("scenic_name"));
                scenicSpot.setScenicAddress(rs.getString("scenic_address"));
                scenicSpot.setPrice(rs.getDouble("price"));
                scenicSpot.setCity(rs.getString("city"));
                order.setScenicSpot(scenicSpot);
                list.add(order);
            }

            // 获取车票订单
            String ticketSql = "SELECT * FROM `tickets`";
            rs = executeQuery(ticketSql);
            while (rs.next()) {
                HzyOrder order = new HzyOrder();
                HzyTickets tickets = new HzyTickets();
                tickets.setId(rs.getInt("id"));
                tickets.setType(rs.getString("type"));
                tickets.setTicketType(rs.getString("ticketType"));
                tickets.setDeparture_city(rs.getString("departure_city"));
                tickets.setArrival_city(rs.getString("arrival_city"));
                tickets.setDeparture_time(rs.getString("departure_date"));
                tickets.setStart_time(rs.getString("start_time"));
                tickets.setEnd_time(rs.getString("end_time"));
                tickets.setPrice(rs.getDouble("price"));
                tickets.setHas_infant(rs.getByte("has_infant_option"));
                tickets.setHas_child(rs.getByte("has_child_option"));
                order.setTickets(tickets);
                list.add(order);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            this.closeAll(conn, psmt, rs);
        }
        return list;
    }

    @Override
    public boolean deleteHotelOrder(Long id) {
        conn = getConnection();
        String sql = "DELETE FROM biaodan WHERE id = ?";
        try {
            return executeUpdate(sql, id) > 0;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            this.closeAll(conn, psmt, rs);
        }
    }

    @Override
    public boolean deleteScenicOrder(Long id) {
        conn = getConnection();
        String sql = "DELETE FROM scenictable WHERE scenic_Id = ?";
        try {
            return executeUpdate(sql, id) > 0;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            this.closeAll(conn, psmt, rs);
        }
    }

    @Override
    public boolean deleteTicketOrder(Long id) {
        conn = getConnection();
        String sql = "DELETE FROM tickets WHERE id = ?";
        try {
            return executeUpdate(sql, id) > 0;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            this.closeAll(conn, psmt, rs);
        }
    }

    @Override
    public boolean updateHotelOrder(HzyHotel hotel) {
        conn = getConnection();
        String sql = "UPDATE biaodan SET roomnumbre=?, contactPhone=?, xmname=?, sfztel=?, prices=?, state=?, brand=? WHERE id=?";
        try {
            return executeUpdate(sql,
                    hotel.getRoomNum(),
                    hotel.getContactPhone(),
                    hotel.getContactName(),
                    hotel.getIdCard(),
                    hotel.getPrice(),
                    hotel.getStatus(),
                    hotel.getBrand(),
                    hotel.getId()) > 0;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            this.closeAll(conn, psmt, rs);
        }
    }

    @Override
    public boolean updateScenicOrder(HzyScenicSpot scenicSpot) {
        conn = getConnection();
        String sql = "UPDATE scenictable SET scenic_name=?, scenic_address=?, price=?, city=? WHERE scenic_Id=?";
        try {
            return executeUpdate(sql,
                    scenicSpot.getScenicName(),
                    scenicSpot.getScenicAddress(),
                    scenicSpot.getPrice(),
                    scenicSpot.getCity(),
                    scenicSpot.getId()) > 0;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            this.closeAll(conn, psmt, rs);
        }
    }

    @Override
    public boolean updateTicketOrder(HzyTickets ticket) {
        conn = getConnection();
        String sql = "UPDATE tickets SET type=?, ticketType=?, departure_city=?, arrival_city=?, departure_date=?, price=?, has_infant_option=?, has_child_option=? WHERE id=?";
        try {
            return executeUpdate(sql,
                    ticket.getType(),
                    ticket.getTicketType(),
                    ticket.getDeparture_city(),
                    ticket.getArrival_city(),
                    ticket.getDeparture_time(),
                    ticket.getPrice(),
                    ticket.getHas_infant(),
                    ticket.getHas_child(),
                    ticket.getId()) > 0;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            this.closeAll(conn, psmt, rs);
        }
    }

    @Override
    public List<HzyOrder> getRecentOrders() {
        conn = getConnection();
        List<HzyOrder> list = new ArrayList<>();

        try {
            // 获取最近1条酒店订单
            String hotelSql = "SELECT * FROM biaodan ORDER BY id DESC LIMIT 1";
            rs = executeQuery(hotelSql);
            while (rs.next()) {
                HzyOrder order = new HzyOrder();
                HzyHotel hotel = new HzyHotel();
                hotel.setId(rs.getInt("id"));
                hotel.setRoomNum(rs.getInt("roomnumbre"));
                hotel.setContactPhone(rs.getString("contactPhone"));
                hotel.setContactName(rs.getString("xmname"));
                hotel.setIdCard(rs.getString("sfztel"));
                hotel.setPrice(rs.getDouble("prices"));
                hotel.setStatus(rs.getString("state"));
                hotel.setBrand(rs.getString("brand"));
                order.setHotel(hotel);
                list.add(order);
            }

            // 获取最近1条景点订单
            String scenicSql = "SELECT * FROM scenictable ORDER BY scenic_Id DESC LIMIT 1";
            rs = executeQuery(scenicSql);
            while (rs.next()) {
                HzyOrder order = new HzyOrder();
                HzyScenicSpot scenicSpot = new HzyScenicSpot();
                scenicSpot.setId(rs.getInt("scenic_Id"));
                scenicSpot.setScenicName(rs.getString("scenic_name"));
                scenicSpot.setScenicAddress(rs.getString("scenic_address"));
                scenicSpot.setPrice(rs.getDouble("price"));
                scenicSpot.setCity(rs.getString("city"));
                order.setScenicSpot(scenicSpot);
                list.add(order);
            }

            // 获取最近1条车票订单
            String ticketSql = "SELECT * FROM tickets ORDER BY id DESC LIMIT 1";
            rs = executeQuery(ticketSql);
            while (rs.next()) {
                HzyOrder order = new HzyOrder();
                HzyTickets tickets = new HzyTickets();
                tickets.setId(rs.getInt("id"));
                tickets.setType(rs.getString("type"));
                tickets.setTicketType(rs.getString("ticketType"));
                tickets.setDeparture_city(rs.getString("departure_city"));
                tickets.setArrival_city(rs.getString("arrival_city"));
                tickets.setDeparture_time(rs.getString("departure_date"));
                tickets.setStart_time(rs.getString("start_time"));
                tickets.setEnd_time(rs.getString("end_time"));
                tickets.setPrice(rs.getDouble("price"));
                tickets.setHas_infant(rs.getByte("has_infant_option"));
                tickets.setHas_child(rs.getByte("has_child_option"));
                order.setTickets(tickets);
                list.add(order);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            this.closeAll(conn, psmt, rs);
        }
        return list;
    }

    @Override
    public boolean updateHotelPayStatus(String orderId, String status) {
        System.out.println("正在更新订单状态：orderId=" + orderId + ", status=" + status);
        conn = getConnection();
        String sql = "UPDATE biaodan SET state = ? WHERE id = ?";

        try {
            int result = executeUpdate(sql, status, orderId);
            System.out.println("更新结果：" + result);
            return result > 0;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            this.closeAll(conn, psmt, rs);
        }
    }

    @Override
    public int getStatusCount() {
        int count = 0;
        conn = getConnection();
        String sql = "SELECT COUNT(*) FROM `biaodan` WHERE `state` = '待付款'";
        try {
            rs = executeQuery(sql);
            if (rs.next()) {
                count = rs.getInt(1);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return count;
    }
}
